﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
namespace RingDDNS
{
    public partial class Form_Options : Form
    {
        private XmlDocument _xmlconfig;
        public Form_Options()
        {
            InitializeComponent();
        }
        public Form_Options(XmlDocument configxml)
        {
            InitializeComponent();
            _xmlconfig = configxml;
            switch (GetGettingIPType())
            { 
                default:
                case "url":
                    rdURL.Checked = true;
                    break;
                case "custom":
                    rdManual.Checked = true;
                    break;
                case "local":
                    rdUseLocalhost.Checked = true;
                    break;
            
            }
            tbxIP.Text = _xmlconfig.SelectSingleNode(@"ring/config/ip/customip") == null ? "" : _xmlconfig.SelectSingleNode(@"ring/config/ip/customip").InnerText;
            tbxLocaIP.Text = Dns.GetHostAddresses("")[0].ToString() ;
            
            XmlNodeList xnl= _xmlconfig.SelectNodes(@"/ring/config/ip/ip_get_urls/url");
            tbxUrls.Text = "";
            foreach (XmlNode ni in xnl)
	        {
                tbxUrls.Text+=ni.InnerText.Trim()+"\r\n";
        	}
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
          XmlNode xn= _xmlconfig.SelectSingleNode(@"/ring/config/ip");
          if (xn != null)
          {
              _xmlconfig.SelectSingleNode(@"/ring/config").RemoveChild(xn);
          }
          CreateIpConfig();
        }

        public XmlNode AddSubTextNode(XmlNode node,string subNodeName,string SubNodeText)
        {
          XmlNode nd= _xmlconfig.CreateElement(subNodeName);
          nd.InnerText= SubNodeText;
          return node.AppendChild(nd);
        }

        /// <summary>
        /// 获得IP获的方式
        /// </summary>
        /// <returns></returns>
        public string GetGettingIPType()
        {
            XmlNode nd = _xmlconfig.SelectSingleNode(@"/ring/config/ip");
            if (nd != null)
            {
                if (nd.Attributes["gettingtype"] != null) return nd.Attributes["gettingtype"].InnerText;
            }

            return "url";
        }
        public void CreateIpConfig()
        {
            StringBuilder sb=new StringBuilder();
            string gettingtype = "url";
            if (rdUseLocalhost.Checked) gettingtype = "local";
            if (rdManual.Checked) gettingtype = "custom";
            if (rdURL.Checked) gettingtype = "url";
            tbxUrls.Text += "\r\n";
            sb.AppendLine("<ip gettingtype=\""+gettingtype+"\"><!--url:url,2:custom,3本机:local-->");
	        sb.AppendLine("<customip>"+tbxIP.Text.Trim(new char[]{' ','\r','\n'})+"</customip>");
            sb.AppendLine("<ip_get_urls>");
            for(int i=0;i<tbxUrls.Lines.Length;i++)
            {
                if (tbxUrls.Lines[i].Trim() != "")
                {
                    sb.AppendLine("<url>" + tbxUrls.Lines[i].Trim() + "</url>");
                }
            }
            sb.AppendLine("</ip_get_urls>");
            sb.AppendLine("</ip>");
            XmlDocumentFragment xdf = _xmlconfig.CreateDocumentFragment();
            xdf.InnerXml = sb.ToString();
            _xmlconfig.SelectSingleNode(@"/ring/config").AppendChild(xdf);
   
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
        
    }
}